package com.thwj.project.app.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thwj.framework.web.domain.R;
import com.thwj.framework.web.service.TokenService;
import com.thwj.project.app.api.param.DynamicCommentParam;
import com.thwj.project.app.api.param.DynamicDeleteCommentParam;
import com.thwj.project.app.api.param.DynamicGoodParam;
import com.thwj.project.app.api.param.DynamicParam;
import com.thwj.project.app.api.vo.DynamicTagVo;
import com.thwj.project.app.api.vo.DynamicVo;
import com.thwj.project.app.service.AppDynamicsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags="app社区动态接口")
@RestController
@RequestMapping("/app/dynamics")
public class AppDynamicController {


    @Autowired
    private AppDynamicsService appDynamicsService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "用户发布动态",notes = "用户发布动态")
    @PostMapping("commitDynamic")
    public R<String> commitDynamic(@RequestBody DynamicParam dynamicParam) {
        appDynamicsService.commitDynamic(dynamicParam, tokenService.getLoginUser());
        return R.data("操作成功");
    }

    @ApiOperation(value = "用户删除动态",notes = "用户删除动态")
    @PostMapping("deleteDynamic")
    public R<String> deleteDynamic(@RequestBody DynamicGoodParam goodParam) {
        appDynamicsService.deleteDynamic(goodParam.getDynamicId(), tokenService.getLoginUser());
        return R.data("删除成功");
    }

    @ApiOperation(value = "用户删除评论",notes = "用户删除评论 这个接口页面有就调用没有的话可以不调用")
    @PostMapping("deleteDynamicComment")
    public R<String> deleteDynamicComment(@RequestBody DynamicDeleteCommentParam commentParam) {
        appDynamicsService.deleteDynamicComment(commentParam.getCommentId(), tokenService.getLoginUser());
        return R.data("删除成功");
    }

    @ApiOperation(value = "获取动态分页列表",notes = "获取动态列表 按照城市自然倒序")
    @GetMapping("getDynamicsTagList")
    public R<List<DynamicTagVo>> getDynamicsTagList() {
        List<DynamicTagVo> tagList = appDynamicsService.getDynamicsTagList();
        return R.data(tagList);
    }

    @ApiOperation(value = "获取动态分页列表",notes = "获取动态列表 按照城市自然倒序")
    @GetMapping("getDynamicsPage")
    public R<Page<DynamicVo>> getDynamicsPage(Page page, @RequestParam(required = false,defaultValue = "330100") Long cityCode) {
        Page<DynamicVo> activityPage = appDynamicsService.getDynamicsPage(page, cityCode, tokenService.getLoginUser());
        return R.data(activityPage);
    }

    @ApiOperation(value = "获取个性化推荐动态分页列表",notes = "实际就是当前用户关注的用户的动态")
    @GetMapping("getUserRecDynamicsPage")
    public R<Page<DynamicVo>> getUserRecDynamicsPage(Page page) {
        Page<DynamicVo> dynamicVoPage = appDynamicsService.getUserRecDynamicsPage(page, tokenService.getLoginUser());
        return R.data(dynamicVoPage);
    }

    @ApiOperation(value = "获取用户自己发布动态分页列表",notes = "获取用户自己发布动态分页列表")
    @GetMapping("getUserDynamicsPage")
    public R<Page<DynamicVo>> getUserDynamicsPage(Page page,@RequestParam(required = false) Long userId) {
        if(userId == null) {
            userId = tokenService.getLoginUser().getUserId();
        }
        Page<DynamicVo> activityPage = appDynamicsService.getUserDynamicsPage(page, userId, tokenService.getLoginUser());
        return R.data(activityPage);
    }

    @ApiOperation(value = "获取动态详情",notes = "这里要注意 实际列表已经返回了详情的数据但是用户只要点击了还是需要请求一遍详情")
    @GetMapping("getDynamicsDetail")
    public R<DynamicVo> getDynamicsDetail(@RequestParam Long dynamicId) {
        DynamicVo dynamicsDetail = appDynamicsService.getDynamicsDetail(dynamicId, tokenService.getLoginUser());
        return R.data(dynamicsDetail);
    }

    @ApiOperation(value = "点赞/取消点赞动态",notes = "点赞/取消点赞动态")
    @PostMapping("commitDynamicGood")
    public R<String> commitDynamicGood(@RequestBody DynamicGoodParam goodParam) {
        appDynamicsService.commitDynamicGood(goodParam.getDynamicId(), tokenService.getLoginUser());
        return R.data("操作成功");
    }

    @ApiOperation(value = "用户评论动态",notes = "用户评论动态")
    @PostMapping("commitDynamicComment")
    public R<String> commitDynamicComment(@RequestBody DynamicCommentParam dynamicCommentParam) {
        appDynamicsService.commitDynamicComment(dynamicCommentParam, tokenService.getLoginUser());
        return R.data("操作成功");
    }

}
